package com.sphd.emanu.fragment.work;


import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.PackageInstaller;
import android.media.tv.TvInputService;
import android.os.Bundle;
import android.service.textservice.SpellCheckerService;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.callback.ItemDragAndSwipeCallback;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.listener.OnItemDragListener;
import com.sphd.emanu.R;
import com.sphd.emanu.activity.finance.FinanceActivity;
import com.sphd.emanu.activity.generalaffairs.GeneralaffairsActivity;
import com.sphd.emanu.activity.goodstype.GoodstypeActivity;
import com.sphd.emanu.activity.marketing.MarketingActivity;
import com.sphd.emanu.activity.orderreceive.OrderreceiveActivity;
import com.sphd.emanu.activity.send.SendActivity;
import com.sphd.emanu.activity.storeroom.StoreroomActivity;
import com.sphd.emanu.activity.takeorder.TakeorderActivity;
import com.sphd.emanu.activity.vip.VipActivity;
import com.sphd.emanu.adapter.WorkFragmentAdapter;
import com.sphd.emanu.config.Constant;


import com.sphd.emanu.mvp.MVPBaseFragment;
import com.sphd.emanu.permission.Moudle;
import com.sphd.emanu.pojo.WorkEntity;
import com.sphd.emanu.utils.PermissionConfig;
import com.wzutils.AppUtils;
import com.wzutils.JsonUtil;
import com.wzutils.SPUtils;
import com.wzutils.T;


import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * @Author Hu
 * @Time 2017/4/7 -- 14:12.
 * @Email 87683202@qq.com
 */

public class WorkFragment extends MVPBaseFragment<WorkContract.View, WorkPresenter> implements OnItemDragListener {
    @BindView(R.id.my_recycler)
    RecyclerView myRecycler;
    Unbinder unbinder;
    WorkFragmentAdapter workFragmentAdapter;
    private ItemTouchHelper mItemTouchHelper;
    private ItemDragAndSwipeCallback mItemDragAndSwipeCallback;
    List<Moudle> moudles;
    private static final String TAG = "WorkFragment";
    String[] titles = {"商品", "接单", "下单",
            "营销", "财务", "会员",
            "资源", "薪资宝", "采购",
            "库房", "总务", "提成",
            "二维码", "分析", "派送"};
    int[] resImg = {R.mipmap.shangpin, R.mipmap.jiedan, R.mipmap.xiadan,
            R.mipmap.yingxiaoshezhi, R.mipmap.caiwu, R.mipmap.huiyuan,
            R.mipmap.ziyuan, R.mipmap.xinzibao, R.mipmap.caigou,
            R.mipmap.cangku, R.mipmap.zongwuguanli, R.mipmap.ticheng,
            R.mipmap.erweima, R.mipmap.fenxi, R.mipmap.yunshu,};
    Bundle bundle;
    Intent intent;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_work;
    }


    private void initEvent() {

        myRecycler.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                WorkEntity workEntity = (WorkEntity) adapter.getItem(position);
                switchType(workEntity.getSrcName());
            }
        });
    }

    private void switchType(String srcName) {
        intent.putExtras(bundle);
        switch (srcName) {
            case "商品":
                if (isCanLook(moudles, PermissionConfig.MENU_FOOD_1)) {
                    intent.setClass(context, GoodstypeActivity.class);
                    startActivity(intent);
                } else {
                    toast(getString(R.string.no_perssion));
                }
                break;
            case "接单":
                if (isCanLook(moudles, PermissionConfig.MENU_RECEIVE_2)) {
                    intent.setClass(context, OrderreceiveActivity.class);
                    startActivity(intent);
                } else {
                    toast(getString(R.string.no_perssion));
                }

                break;
            case "下单":
                if (isCanLook(moudles, PermissionConfig.MENU_ORDERS_3)) {
                    intent.setClass(context, TakeorderActivity.class);
                    startActivity(intent);
                } else {
                    toast(getString(R.string.no_perssion));
                }
                break;
            case "营销":
                if (isCanLook(moudles, PermissionConfig.MENU_MAKETING_4)) {
                    intent.setClass(context, MarketingActivity.class);
                    startActivity(intent);
                } else {
                    toast(getString(R.string.no_perssion));
                }
                break;
            case "财务":
                if (isCanLook(moudles, PermissionConfig.MENU_FINANCE_5)) {
                    intent.setClass(context, FinanceActivity.class);
                    startActivity(intent);
                } else {
                    toast(getString(R.string.no_perssion));
                }

                break;
            case "会员":
                if (isCanLook(moudles, PermissionConfig.MENU_VIP_6)) {
                    intent.setClass(context, VipActivity.class);
                    startActivity(intent);
                } else {
                    toast(getString(R.string.no_perssion));
                }
                break;
            case "资源":
                break;
            case "薪资宝":
                try {
                    intent.setAction(Intent.ACTION_MAIN);
                    ComponentName componentName = new ComponentName("com.sphd.xinzibao.activitys",
                            "com.sphd.xinzibao.activitys.PayTreasureFirstActivity");
                    intent.setComponent(componentName);
                    startActivity(intent);
                } catch (Exception e) {
                    toast("手机未安装“薪资宝”应用");
                }
                break;
            case "采购":
                break;
            case "库房":
                if (isCanLook(moudles, PermissionConfig.MENU_STOREHOUSE_8)) {
                    intent.setClass(context, StoreroomActivity.class);
                    startActivity(intent);
                } else {
                    toast(getString(R.string.no_perssion));
                }

                break;
            case "总务":
                if (isCanLook(moudles, PermissionConfig.MENU_ZONGWU_13)) {
                    intent.setClass(context, GeneralaffairsActivity.class);
                    startActivity(intent);
                } else {
                    toast(getString(R.string.no_perssion));
                }
                break;
            case "提成":
                break;
            case "二维码":
                break;
            case "分析":
                break;
            case "派送":
                if (isCanLook(moudles, PermissionConfig.MENU_SENDING_9)) {
                    intent.setClass(context, SendActivity.class);
                    startActivity(intent);
                } else {
                    toast(getString(R.string.no_perssion));
                }

                break;
            default:
                T.show(context, "暂无该模块", Toast.LENGTH_SHORT);
                break;

        }
    }

    @Override
    protected void initView() {
        unbinder = ButterKnife.bind(this, rootView);
        intent = new Intent();
        bundle = getArguments();
        moudles = (List<Moudle>) bundle.getSerializable(Constant.PERMISSION_LIST);
        initEvent();

    }

    @Override
    protected void initData() {
        initAdapter();
    }

    private void initAdapter() {
        List<WorkEntity> entityList = addData();

        setGRecy(myRecycler, 3);

        workFragmentAdapter = new WorkFragmentAdapter(R.layout.fragment_work_item, entityList);
        workFragmentAdapter.setEmptyView(emptyView);
        mItemDragAndSwipeCallback = new ItemDragAndSwipeCallback(workFragmentAdapter);
        mItemTouchHelper = new ItemTouchHelper(mItemDragAndSwipeCallback);
        mItemTouchHelper.attachToRecyclerView(myRecycler);
        workFragmentAdapter.enableDragItem(mItemTouchHelper);
        workFragmentAdapter.setOnItemDragListener(this);
        myRecycler.setAdapter(workFragmentAdapter);

    }

    private List<WorkEntity> addData() {
        Log.e("====ADD==", "======");
        List<WorkEntity> entityList = new ArrayList<>();
        if (SPUtils.contains(context, Constant.WORK_LIST)) {
            if (AppUtils.getVersionName(context).equals(SPUtils.get(context, Constant.VER_NO, ""))) {
                List<WorkEntity> workEntities = JsonUtil.getListFromString((String) get(Constant.WORK_LIST, ""), WorkEntity.class);
                for (WorkEntity workEntity : workEntities) {
                    workEntity.setCanLook(isCanLook(moudles, getMenuId(workEntity.getSrcName())));
                    entityList.add(workEntity);
                }
            } else {
                for (int i = 0; i < titles.length; i++) {
                    WorkEntity workEntity = new WorkEntity();
                    workEntity.setSrcId(resImg[i]);
                    workEntity.setSrcName(titles[i]);
                    workEntity.setCanLook(isCanLook(moudles, getMenuId(titles[i])));
                    workEntity.setMenuId(getMenuId(titles[i]));
                    entityList.add(workEntity);
                }
                put(Constant.WORK_VER_NO, AppUtils.getVersionName(context));
            }
        } else {
            for (int i = 0; i < titles.length; i++) {
                WorkEntity workEntity = new WorkEntity();
                workEntity.setSrcId(resImg[i]);
                workEntity.setSrcName(titles[i]);
                workEntity.setCanLook(isCanLook(moudles, getMenuId(titles[i])));
                workEntity.setMenuId(getMenuId(titles[i]));
                entityList.add(workEntity);
            }
            put(Constant.WORK_VER_NO, AppUtils.getVersionName(context));
        }
        return entityList;
    }


    @Override
    public void onItemDragStart(RecyclerView.ViewHolder viewHolder, int pos) {

    }

    @Override
    public void onItemDragMoving(RecyclerView.ViewHolder source, int from, RecyclerView.ViewHolder target, int to) {

    }

    @Override
    public void onItemDragEnd(RecyclerView.ViewHolder viewHolder, int pos) {
        List<WorkEntity> works = workFragmentAdapter.getData();
        put(Constant.WORK_LIST, JsonUtil.getStringFromList(works));
    }

    public int getMenuId(String title) {
        int flag = 0;
        switch (title) {
            case "商品":
                flag = PermissionConfig.MENU_FOOD_1;
                break;
            case "接单":
                flag = PermissionConfig.MENU_RECEIVE_2;
                break;
            case "下单":
                flag = PermissionConfig.MENU_ORDERS_3;
                break;
            case "营销":
                flag = PermissionConfig.MENU_MAKETING_4;
                break;
            case "财务":
                flag = PermissionConfig.MENU_FINANCE_5;

                break;
            case "会员":
                flag = PermissionConfig.MENU_VIP_6;
                break;
            case "资源":

                break;
            case "薪资宝":
                    flag=PermissionConfig.MENU_XINZIBAO_14;
                break;
            case "采购":
                flag = PermissionConfig.MENU_BUY_7;
                break;
            case "库房":
                flag = PermissionConfig.MENU_STOREHOUSE_8;
                break;
            case "总务":
                flag = PermissionConfig.MENU_ZONGWU_13;
                break;
            case "提成":
                break;
            case "二维码":

                break;
            case "分析":

                break;
            case "派送":
                flag = PermissionConfig.MENU_SENDING_9;
                break;
            default:
                T.show(context, "暂无该模块", Toast.LENGTH_SHORT);
                break;
        }
        return flag;
    }

}
